package DB;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;



public class VO {
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	Calendar cal = Calendar.getInstance();
	String today = formatter.format(cal.getTime());
	Timestamp ts = Timestamp.valueOf(today);
	int wallIndex = 0;
	int windowIndex = 1;

	ArrayList<tb_biomass> biomass;
	ArrayList<tb_chp> chp;
	ArrayList<tb_coal> coal;
	ArrayList<tb_cooling> cooling;
	ArrayList<tb_fuelcell> fuelcell;
	ArrayList<tb_geoheatpump> geoheatpump;
	ArrayList<tb_heating> heating;
	ArrayList<tb_hvac> hvac;
	ArrayList<tb_hws> hws;
	ArrayList<tb_light> light;
	ArrayList<tb_micro> micro;
	ArrayList<tb_ocean> ocean;
	ArrayList<tb_project> project;
	ArrayList<tb_pv> pv;
	ArrayList<tb_solarheat> solarheat;
	ArrayList<tb_trash> trash;
	ArrayList<tb_wall> wall;
	ArrayList<tb_water> water;
	ArrayList<tb_window> window;
	ArrayList<tb_windturbine> windturbine;
	ArrayList<tb_zone> zone;
	ArrayList<String> wallName;

	public VO() {
		biomass = new ArrayList<tb_biomass>();
		chp = new ArrayList<tb_chp>();
		coal = new ArrayList<tb_coal>();
		cooling = new ArrayList<tb_cooling>();
		fuelcell = new ArrayList<tb_fuelcell>();
		geoheatpump = new ArrayList<tb_geoheatpump>();
		heating = new ArrayList<tb_heating>();
		hvac = new ArrayList<tb_hvac>();
		hws = new ArrayList<tb_hws>();
		light = new ArrayList<tb_light>();
		micro = new ArrayList<tb_micro>();
		ocean = new ArrayList<tb_ocean>();
		project = new ArrayList<tb_project>();
		pv = new ArrayList<tb_pv>();
		solarheat = new ArrayList<tb_solarheat>();
		trash = new ArrayList<tb_trash>();
		wall = new ArrayList<tb_wall>();
		water = new ArrayList<tb_water>();
		window = new ArrayList<tb_window>();
		windturbine = new ArrayList<tb_windturbine>();
		zone = new ArrayList<tb_zone>();
		wallName = new ArrayList<String>();
	}

	private Double parseDouble(String data) {
		
		String temp = strTonum(data);
		if (temp.equals("")) {
			return (double) 0;
		} else {
			return Double.parseDouble(temp.trim());
		}
	}

	private int parseInt(String data) {
		double temp;
		temp = parseDouble(data);
		
		if (data.equals("")) {
			return (int) 0;
		} else {
			return (int) temp;
		}
	}
	
	private String strTonum(String str) {

		if (str == null) {
			return "0";
		}
		int col_count = 0;
		col_count = str.indexOf('.');

		String result = "";

		for (int i = 0; i < str.length(); i++) {
			if (i == col_count) {
				result = result + '.';
			} else if (Character.isDigit(str.charAt(i))) {
				result = result + str.charAt(i);
			}

		}

		if (result.equals("")) {
			return "0";
		}
		return result;
	}

	public void addProject(String data[]) {
		tb_project temp = new tb_project();
		temp.setTxtProjectNm(data[0]);
		temp.setTxtInputer(data[1]);
		temp.setTxtGroup(data[2]);
		temp.setTxtCallNumber(data[3]);
		temp.setTxtDate(data[4].replaceAll("\"", ""));
		temp.setCboLocation(data[5]);
		temp.setCboUse(data[7]);
		temp.setNumArea(parseDouble(data[8]));
		temp.setTxtAddress(data[6]);
		temp.setNumFloor(parseInt(data[9]));
		temp.setUpdate_date(ts);

		this.project.add(temp);
	}

	public void addEcoEnergy(String data[]) {
		tb_solarheat tempsolarheat = new tb_solarheat();
		tempsolarheat.setCboKind(data[0]);
		tempsolarheat.setTxtArea(parseDouble(data[1]));
		tempsolarheat.setUpdate_date(ts);
		if (tempsolarheat.getTxtArea() != 0)
			this.solarheat.add(tempsolarheat);
		tb_pv temppv = new tb_pv();
		temppv.setCboKind(data[2]);
		temppv.setTxtArea(parseDouble(data[3]));
		temppv.setUpdate_date(ts);
		if (temppv.getTxtArea() != 0)
			this.pv.add(temppv);
		tb_geoheatpump tempgeoheatpump = new tb_geoheatpump();
		tempgeoheatpump.setCapacity(parseDouble(data[4]));
		tempgeoheatpump.setHeatingCOP(parseDouble(data[5]));
		tempgeoheatpump.setCoolingCOP(parseDouble(data[6]));
		tempgeoheatpump.setUpdate_date(ts);
		if (tempgeoheatpump.getCapacity() != 0)
			this.geoheatpump.add(tempgeoheatpump);
		tb_fuelcell tempfuelcell = new tb_fuelcell();
		tempfuelcell.setCapacity(parseDouble(data[7]));
		tempfuelcell.setEfficiency(parseDouble(data[8]));
		tempfuelcell.setUpdate_date(ts);
		if (tempfuelcell.getCapacity() != 0)
			this.fuelcell.add(tempfuelcell);
		tb_windturbine tempwindturbine = new tb_windturbine();
		tempwindturbine.setCapacity(parseDouble(data[9]));
		tempwindturbine.setEfficiency(parseDouble(data[10]));
		tempwindturbine.setUpdate_date(ts);
		if (tempwindturbine.getCapacity() != 0)
			this.windturbine.add(tempwindturbine);
		
		tb_biomass tempbiomass = new tb_biomass();
		tempbiomass.setCapacity(parseDouble(data[11]));
		tempbiomass.setHeatEfficiency(parseDouble(data[12]));	
		tempbiomass.setUpdate_date(ts);
		if (tempbiomass.getCapacity() != 0)
			this.biomass.add(tempbiomass);
		
		if (data[13].equals("O")) {
			tb_micro tempmicro = new tb_micro();
			this.micro.add(tempmicro);
		}
		if (data[14].equals("O")) {
			tb_ocean tempocean = new tb_ocean();
			this.ocean.add(tempocean);
		}
		if (data[15].equals("O")) {
			tb_trash temptrash = new tb_trash();
			this.trash.add(temptrash);
		}
		if (data[16].equals("O")) {
			tb_coal tempcoal = new tb_coal();
			this.coal.add(tempcoal);
		}
		if (data[17].equals("O")) {
			tb_water tempwater = new tb_water();
			this.water.add(tempwater);
		}
		tb_chp tempchp = new tb_chp();
		tempchp.setType(data[18]);
		tempchp.setFuelType(data[19]);
		tempchp.setHeatEfficiency(parseDouble(data[20]));
		tempchp.setElectricEfficiency(data[21]);
		tempchp.setUpdate_date(ts);
		if (tempchp.getHeatEfficiency() != 0)
			this.chp.add(tempchp);
	}

	public void addZoneData(String data[], int zoneIndex) {
		tb_light templight = new tb_light();
		templight.setZoneIdx(zoneIndex);
		templight.setCboLightKind(data[1]);
		templight.setNumLSCnt(parseInt(data[2]));
		templight.setUpdate_date(ts);
		light.add(templight);
		tb_hvac temphavc = new tb_hvac();
		temphavc.setZoneIdx(zoneIndex);
		temphavc.setCboHVACKind(data[3]);
		temphavc.setCboHeatRecover(data[4]);
		temphavc.setUpdate_date(ts);
		hvac.add(temphavc);
		tb_hws temphws = new tb_hws();
		temphws.setZoneIdx(zoneIndex);
		temphws.setCboHeatingKind(data[5]);
		temphws.setCOP(parseDouble(data[6]));
		temphws.setCboFuelType(data[7]);
		temphws.setUpdate_date(ts);
		hws.add(temphws);
		tb_heating tempheating = new tb_heating();
		tempheating.setZoneIdx(zoneIndex);
		tempheating.setCboHeatingKind(data[8]);
		tempheating.setCOP(parseDouble(data[9]));
		tempheating.setCboFuelType(data[10]);
		tempheating.setUpdate_date(ts);
		heating.add(tempheating);
		tb_cooling tempcooling = new tb_cooling();
		tempcooling.setZoneIdx(zoneIndex);
		tempcooling.setCboCoolingKind(data[11]);
		tempcooling.setCOP(parseDouble(data[12]));
		tempcooling.setCboFuelType(data[13]);
		tempcooling.setUpdate_date(ts);
		cooling.add(tempcooling);
	}

	public boolean addBuildinginform(String data[], int zoneIndex) {
		tb_zone tempzone = new tb_zone();
		tempzone.setZoneIdx(zoneIndex);
		tempzone.setZoneName(data[0]);
		if (data[1].equals("") ){
			return false;
		}
		else{
			tempzone.setCboUse(data[1]);
			tempzone.setHeight(data[2]);
			tempzone.setAfBottom(parseDouble(data[3]));
			tempzone.setStructure(data[4]);
			tempzone.setNumber(parseInt(data[5]));
			tempzone.setUpdate_date(ts);
			zone.add(tempzone);
			return true;
		}
	}

	/**
	 * 
	 * 중간에 if else문이 원하는 대로 잘 돌아가는지 확인 아직 안되어 있음
	 * 	
	 *  */
	public void addWallData(String data[], String kind, int zoneIndex) {
		tb_wall tempwall = new tb_wall();
		tb_window tempwindow = new tb_window();
		for (int i = 1; i < data.length; i++) {
			int mod = (i - 1) % 10;
			if (mod == 0) {
				tempwall = new tb_wall();
				tempwall.setName(data[i]);
				tempwall.setKind(kind);
			} else if (mod == 1) {
				tempwall.setCboWall(data[i]);
			} else if (mod == 2) {
				tempwall.setCboBearing(data[i]);
			} else if (mod == 3) {
				tempwall.setNumArea(data[i]);
			} else if (mod == 4) {
				tempwall.setNumHeatRate(data[i]);
				tempwall.setUpdate_date(ts);
				tempwall.setZoneIdx(zoneIndex);
				tempwall.setWallIdx(wallIndex);
				if(!tempwall.getName().equals("")){
					wall.add(tempwall);
					wallIndex++;
				}
				else{
					continue;
				}
			} else if (mod == 5) {
				tempwindow = new tb_window();
				tempwindow.setCboWindowKind(data[i]);
			} else if (mod == 6) {
				tempwindow.setHrpHeatRate(parseDouble(data[i]));
			} else if (mod == 7) {
				tempwindow.setAfBottom(data[i]);
			} else if (mod == 8) {
				tempwindow.setCboWindowFrameKind(data[i]);
			} else if (mod == 9) {
				tempwindow.setTxtGeff(data[i]);
				tempwindow.setUpdate_date(ts);
				tempwindow.setZoneIdx(zoneIndex);
				tempwindow.setWallIdx(wallIndex-1);
				tempwindow.setWindowIdx(windowIndex);
				if (!tempwindow.getCboWindowKind().equals("")) {
					window.add(tempwindow);
					windowIndex++;
				}

			}

		}
	}

	
	public ArrayList<String> getWallName() {
		return wallName;
	}

	public void setWallName(ArrayList<String> wallName) {
		this.wallName = wallName;
	}

	public ArrayList<tb_biomass> getBiomass() {
		return biomass;
	}

	public void setBiomass(ArrayList<tb_biomass> biomass) {
		this.biomass = biomass;
	}

	public ArrayList<tb_chp> getChp() {
		return chp;
	}

	public void setChp(ArrayList<tb_chp> chp) {
		this.chp = chp;
	}

	public ArrayList<tb_coal> getCoal() {
		return coal;
	}

	public void setCoal(ArrayList<tb_coal> coal) {
		this.coal = coal;
	}

	public ArrayList<tb_cooling> getCooling() {
		return cooling;
	}

	public void setCooling(ArrayList<tb_cooling> cooling) {
		this.cooling = cooling;
	}

	public ArrayList<tb_fuelcell> getFuelcell() {
		return fuelcell;
	}

	public void setFuelcell(ArrayList<tb_fuelcell> fuelcell) {
		this.fuelcell = fuelcell;
	}

	public ArrayList<tb_geoheatpump> getGeoheatpump() {
		return geoheatpump;
	}

	public void setGeoheatpump(ArrayList<tb_geoheatpump> geoheatpump) {
		this.geoheatpump = geoheatpump;
	}

	public ArrayList<tb_heating> getHeating() {
		return heating;
	}

	public void setHeating(ArrayList<tb_heating> heating) {
		this.heating = heating;
	}

	public ArrayList<tb_hvac> getHvac() {
		return hvac;
	}

	public void setHvac(ArrayList<tb_hvac> hvac) {
		this.hvac = hvac;
	}

	public ArrayList<tb_hws> getHws() {
		return hws;
	}

	public void setHws(ArrayList<tb_hws> hws) {
		this.hws = hws;
	}

	public ArrayList<tb_light> getLight() {
		return light;
	}

	public void setLight(ArrayList<tb_light> light) {
		this.light = light;
	}

	public ArrayList<tb_micro> getMicro() {
		return micro;
	}

	public void setMicro(ArrayList<tb_micro> micro) {
		this.micro = micro;
	}

	public ArrayList<tb_ocean> getOcean() {
		return ocean;
	}

	public void setOcean(ArrayList<tb_ocean> ocean) {
		this.ocean = ocean;
	}

	public ArrayList<tb_project> getProject() {
		return project;
	}

	public void setProject(ArrayList<tb_project> project) {
		this.project = project;
	}

	public ArrayList<tb_pv> getPv() {
		return pv;
	}

	public void setPv(ArrayList<tb_pv> pv) {
		this.pv = pv;
	}

	public ArrayList<tb_solarheat> getSolarheat() {
		return solarheat;
	}

	public void setSolarheat(ArrayList<tb_solarheat> solarheat) {
		this.solarheat = solarheat;
	}

	public ArrayList<tb_trash> getTrash() {
		return trash;
	}

	public void setTrash(ArrayList<tb_trash> trash) {
		this.trash = trash;
	}

	public ArrayList<tb_wall> getWall() {
		return wall;
	}

	public void setWall(ArrayList<tb_wall> wall) {
		this.wall = wall;
	}

	public ArrayList<tb_water> getWater() {
		return water;
	}

	public void setWater(ArrayList<tb_water> water) {
		this.water = water;
	}

	public ArrayList<tb_window> getWindow() {
		return window;
	}

	public void setWindow(ArrayList<tb_window> window) {
		this.window = window;
	}

	public ArrayList<tb_windturbine> getWindturbine() {
		return windturbine;
	}

	public void setWindturbine(ArrayList<tb_windturbine> windturbine) {
		this.windturbine = windturbine;
	}

	public ArrayList<tb_zone> getZone() {
		return zone;
	}

	public void setZone(ArrayList<tb_zone> zone) {
		this.zone = zone;
	}
}
